/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.web.page.component;

import com.rafkind.resrev.dao.ReservationDao;
import com.rafkind.resrev.data.Reservation;
import com.rafkind.resrev.data.Resource;
import com.rafkind.resrev.web.page.CalendarPage;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Fictional Hero
 */
public class EditOrDeleteReservationPanel extends Panel {
  
  @SpringBean
  private ReservationDao reservationDao;
  
  public EditOrDeleteReservationPanel(String id, 
      IModel<Reservation> model) {
    super(id, model);
    
    Link editLink = new Link("editLink"){
      @Override
      public void onClick() {
        setResponsePage(
            new CalendarPage(
              new Model(
                (Reservation) EditOrDeleteReservationPanel.this.getDefaultModelObject()), 
                getPage()));
      }      
    };
    Link deleteLink = new Link("deleteLink"){
      @Override
      public void onClick() {
        reservationDao.deleteReservation((Reservation) EditOrDeleteReservationPanel.this.getDefaultModelObject());
      }      
    };
    
    Reservation r = model.getObject();
    String message = 
        String.format(
          "return confirm('Really delete {0}'s reservation for "
          + "{1} from {2} to {3}?');",
            r.getReserver().getName(),
            r.getResource().getName(),
            r.getStart(),
            r.getEnd());
    
    deleteLink.add(new AttributeAppender("onclick", new Model(message), ";"));
    
    add(editLink);
    add(deleteLink);
    
  }
}